<template>
    <div>

        <el-breadcrumb separator-icon="ArrowRight" style="margin: 16px">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>报修申请</el-breadcrumb-item>
        </el-breadcrumb>

        <el-card style="margin: 15px; min-height: calc(100vh - 111px)">
            <div class="box">
                <div class="items" style="width: 400px;">
                    <p style="margin-bottom: 50px;">申请表单</p>
                    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                       
                        <el-form-item label="学号" prop="stuId">
                           <el-input v-model="form.stuId" style="width: 80%"></el-input>
                        </el-form-item>
                        <el-form-item label="姓名" prop="stuName">
                            <el-input v-model="form.stuName" style="width: 80%"></el-input>
                        </el-form-item>
                        <el-form-item label="房间号" prop="roomId">
                            <el-input v-model="form.roomId" style="width: 80%"></el-input>
                        </el-form-item>
                        <el-form-item label="楼栋号" prop="ldId">
                            <el-input v-model="form.ldId" style="width: 80%"></el-input>
                        </el-form-item>
                        <el-form-item label="故障物品" prop="rpItem">
                            <el-input v-model="form.rpItem" style="width: 80%"></el-input>
                        </el-form-item>
                        <el-form-item label="故障原因" prop="rpReason">
                            <el-input  type="textarea" :row="3" placeholder="请输入内容" v-model="form.rpReason" style="width: 80%"></el-input>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="onSubmit">提交申请</el-button>
                            <el-button @click="reset">重置</el-button>
                        </el-form-item>
                    </el-form>

                </div>
                <div class="items" style="width:800px;">
                    <p>历史申请</p>
                    <el-table v-loading="loading" :data="tableData"  max-height="1000" style="width: 100%">
                        <el-table-columm v-if ="false" label="编号" prop="rpId"></el-table-columm>
                        <el-table-column label="申请时间" prop="rpDate" width="100px"/>
                        <el-table-column label="故障物品" prop="rpItem" width="100px"/>
                        <el-table-column label="申请原因" prop="rpReason"/>
                        <el-table-column label="审核状态" prop="rpStatus" :formatter="parseStatus" width="100px">
                            <template slot-scope="scope">
                                <el-tag v-if = "!scope.row.rpStatus" type="info" effect="dark">还未审核</el-tag>
                                <el-tag v-else-if = "scope.row.rpStatus" type="success" effect="dark">已通知维修工</el-tag>
                             </template>
                        </el-table-column>
                        <el-table-column label="完成状态" width="150px" prop="rpStatus" :formatter="parseFinish" >
                                <template slot-scope="scope">
                                    <el-tag
                                    type="info" effect="dark"
                                    v-if="!scope.row.rpStatus">还未审核</el-tag>
                                    <el-tag
                                    type="danger" effect="dark"
                                    v-else-if="!scope.row.isFinish">未完成</el-tag>
                                    <el-tag
                                    type="success" effect="dark"
                                    v-else-if="scope.row.isFinish">已完成</el-tag>
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" width="100px" >
                            <template slot-scope="scope">
                                <el-button
                                size="mini"
                                type="success"
                                @click="handleEdit(scope.$index, scope.row)">确认完成</el-button>
                          </template>
                         </el-table-column>
                    </el-table>
                </div>
            </div>
            

        </el-card>
    </div>
 
 

</template>

<script>
import request from '@/utils/request';
export default {
    name:"applyRepairInfo",
    data(){

        return {
            status:"",
            loading:true,
            form:{
                stuId:null,
                stuName:null,
                roomId:null,
                ldId:null,
                rpItem:"",
                rpReason:"",
                rpDate:null
            },
            tableData:[
                {
                    rpId:null,
                    rpDate:null,
                    rpReason:null,
                    rpItem:null,
                    rpStatus:null,
                    isFinish:null
                }
            ],
           
            rules: {
                stuId: [
                    {required: true, message: "请输入学号", trigger: "blur"},
                ],
                ldId: [
                    {required: true, message: "请输入楼栋号", trigger: "blur"},
                ],
                stuName: [
                    {required: true, message: "请输入姓名", trigger: "blur"},
                ],
                roomId:[{
                    required: true, message: "请填写房间号", trigger: "blur"
                }],
                rpReason:[{
                    required: true, message: "请填写故障原因", trigger: "blur"
                }],
                rpItem:[{
                    required: true, message: "请写明故障物品", trigger: "blur"
                }]
               
            },
            stuid:null,
        }
    },
    created(){
        this.stuid=JSON.parse(window.localStorage.getItem('user')).stuId;
        this.load();
        this.loading = true;
        setTimeout(() => {
            //设置延迟执行
            this.loading = false;
        }, 1000);
    },
    methods:{
        async load(){
            this.stuid = parseInt(this.stuid)
            request.get("/repair/findByStuId", {
                params:{
                    stuId:this.stuid
                }
            }).then(res=>{
                if(res.code==="1"){
                    this.tableData=res.data;
                    this.loading=false;
                }
            })
        },
        parseStatus(row) {
         if(!row.rpStatus){
           return "未审核"
         }
         else if(row.rpStatus){
            return '已审核'
         }
        
        },
        parseFinish(row){
            if(row.isFinish){
                return "已完成"
            }
            else{
                return "未完成"
            }
        },
        async onSubmit(){
            this.$refs.form.validate(vaild=>{
                if(vaild){
                    request.post("/repair/applyadd",this.form).then(res=>{
                        if(res.code==="1"){
                            this.$message({
                            type:'success',
                            message:'成功提交申请'
                            })
                            this.load();
                            this.form={};
                        }
                        else{
                            this.$message({
                            type:'error',
                            message:res.msg
                            })
                        }
                    })
                }

            })
        },
        reset(){
            this.form={}
        },
        async handleEdit(index,row){
            if(!row.rpStatus){
                this.$message({
                    type:"error",
                    message:"还未审核不能确认完成！！"
                })
            }
            else{
                var tempData={
                    rpId:null,
                    rpDate:null,
                    rpReason:null,
                    rpStatus:null,
                    isFinish:null
                }
                tempData =  this.form = JSON.parse(JSON.stringify(row));
                request.put("/repair/updateFinish",tempData).then(res=>{
                        if(res.code==="1"){
                            this.load();
                        }
                        else{
                            this.$message({
                    type:"error",
                    message:"修改错误"
                })
                        }
                })
            }
        }
    }
}
</script>

<style scoped>
    .box {
        margin-top:10px;
        display: flex;
        justify-content: space-between;
        flex-wrap: wrap;
    }
    .items {
        border-radius: 10px;
        justify-content: space-between;
      
       
        text-align: center;
        line-height:30px;
        margin-left: calc(4% / 3);
        margin-bottom: calc(4% / 3);
      
    }
</style>